﻿using System;
using System.Collections.Generic;
using System.Text;
using DataPackage;

namespace CostomGameServer
{
	/// <summary>
	/// 处理服务器上所有的Package
	/// </summary>
	static class PackageManager
	{
		private static List<Package> packages = new List<Package>( );
		private static Package current;
		internal static Package Refusal = new Package( ) { Content = CommandType.ServerRefuse.GetHashCode( ) };
		static int tCurrent = -1;
		static internal bool isActive = false;
		static internal bool inGame = false;

		static internal void Start() {
			isActive = true;
			current = new Package( );

			ServerTick.Tick += Tick;
		}

		static void Tick() {
			tCurrent++;
			packages.Add( current );
			current = new Package( );
		}

		/// <summary>
		/// 将Package按照时间戳合并到相应大包中
		/// </summary>
		/// <param name="p"></param>
		static internal void Merge(Package p) {
			lock (packages[ p.Timestamp ]) {
				packages[ p.Timestamp ].Command.AddRange( p.Command );
			}
		}

		static internal Package GetCurrent() {
			return packages[ tCurrent ];
		}

		static internal Package GetPackageByTime(int timestamp) {
			return packages[ timestamp ];
		}

		internal static void Parse(Package package) {
			int c = package.Content;
			int svrpkgs = -1;
			//分析一哈这个数据包里都有啥跟服务器相关的数据
			if ((c & CommandType.HandShake) != 0) {
				svrpkgs++;
				//HandShake();
			}
			if ((c & CommandType.LogIn) != 0) {
				svrpkgs++;
				if (AccountManager.HasAccount( (int)package.Command[ svrpkgs ].X )) {
					Console.WriteLine( "wow" );
				}
			}
			if ((c & CommandType.LogOut) != 0) {

			}
			if ((c & CommandType.PullRecord) != 0) {

			}
			if ((c & CommandType.ServerRefuse) != 0) {

			}
			if ((c & CommandType.MoveAndView) != 0) {

			}

		}
	}
}
